﻿using QingZhouLinYe_Web.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

namespace QingZhouLinYe_Web.DbContext
{
    public class TestServices
    {
        public async Task<List<HuoQing>> getHuoQingByZhuangTaiId(int ztid, int fhdwid, int authority)
        {
            using (SqlSugarClient db = DbHelper.GetDb())
            {
                List<HuoQing> hqs = new List<HuoQing>();
                try
                {
                    //db.Ado.BeginTran();
                    Debug.WriteLine("获取火情事务开始：");
                    if (authority == 0)
                    {
                        hqs = await db.Queryable<HuoQing>()
                           .Includes(hq => hq.HuoQingLeiBie)
                           .Includes(hq => hq.HuoQingZhiBei)
                           .Includes(hq => hq.FangHuoDanWei)
                           .Includes(hq => hq.SheBei)
                           .Includes(hq => hq.HuoQingZhuangTai)
                           .Where(hq => hq.HuoQingZhuangTaiId == ztid)
                           .ToListAsync();
                        //db.Ado.CommitTran();
                        Debug.WriteLine("完成获取火情事务："+hqs.Count);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("火情事务报错：" + ex);
                    //db.Ado.RollbackTran();

                }
                return hqs;
            }
        }

        public async Task<List<SheBei>> GetSheBeis(int authority, int fhdwid, int sblxid)
        {
            using (SqlSugarClient db = DbHelper.GetDb())
            {
                List<SheBei> sbs = new List<SheBei>();
                try
                {
                    //db.Ado.BeginTran();
                    Debug.WriteLine("开启获取设备事务："+authority);
                    if (authority == 0)
                    {
                        sbs = await db.Queryable<SheBei>()
                        .Includes(sb => sb.FangHuoDanWei)
                        .Includes(sb => sb.SheBeiLeiXing)
                        .Where(sb => sb.LeiXingId == sblxid)
                        .ToListAsync();
                        //db.Ado.CommitTran();
                        Debug.WriteLine("超级权限完成事务："+sbs.Count);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("错误事务：" + ex);
                    //db.Ado.RollbackTran();
                }
                return sbs;
            }
        }
    }
}